package model

import (
	"stock/global"
	"sync"

	"gorm.io/gorm"
)

type PurchaseOrderModel struct {
	Base
}

type MallPurchaseOrder struct {
	Id             uint   `json:"id" gorm:"<-:false; primaryKey;column:id;type:int(11)"`
	OrderNo        string `json:"order_no" gorm:"<-:false; column:order_no;"`
	PurchaseId     uint   `json:"purchase_id" gorm:"<-:false; column:purchase_id;"`
	Num            uint   `json:"num" gorm:"column:num;"`
	Remark         string `json:"remark" gorm:"column:remark;"`
	State          uint8  `json:"state" gorm:"column:state;"`
	CreateTime     uint   `json:"create_time" gorm:"column:create_time;"`
	CountryCode    string `json:"country_code" gorm:"<-:false; column:country_code;"`
	CategoryId     uint   `json:"category_id" gorm:"column:category_id;"`
	CheckState     uint8  `json:"check_state" gorm:"column:check_state;"`
	ExpirationDate uint   `json:"expiration_date" gorm:"column:expiration_date;"`
	// PurchaseOrderCover string `json:"purchase_order_cover"`
	IsRecommend       uint8  `json:"is_recommend" gorm:"column:is_recommend;"`
	IsUp              uint8  `json:"is_up" gorm:"column:is_up;"`
	IsDelete          uint8  `json:"is_delete" gorm:"column:is_delete;"`
	PurchaseOrderLang string `json:"lang" gorm:"column:lang;"`
	Perfect           string `json:"perfect" gorm:"column:perfect;"`
}

type RequestPurchaseOrder struct {
	OrderNo        string
	MerchantId     uint
	MerchantType   uint8
	State          uint8
	CountryCode    string
	CategoryId     uint
	CheckState     uint8
	ExpirationDate uint
	IsRecommend    uint8
	IsUp           uint8
	IsDelete       uint8
	Perfect        []string
	TableId        uint
	IdArr          []uint
}

type ResponsePurchaseOrder struct {
	MallPurchaseOrder
	ResponseFile
	ResponseLang
	PurchaseDetail string `json:"purchase_detail"`
	// MainImg            string `json:"main_img"`
	// PurchaseDetail string `json:"purchase_detail" gorm:"column:purchase_detail;"`
	// PurchaseDetail string `json:"purchase_detail"`
}

func (m *PurchaseOrderModel) TabName() string {
	return global.CONF.Mysql.Prefix + "mall_purchase_order"
}

// 列表
func (m *PurchaseOrderModel) List(param *RequestPurchaseOrder, page global.PageSort) ([]ResponsePurchaseOrder, error) {

	where := &MallPurchaseOrder{}
	if param.MerchantId != 0 {
		where.PurchaseId = param.MerchantId
	}
	if param.OrderNo != "" {
		where.OrderNo = param.OrderNo
	}
	if param.CheckState != 0 {
		where.CheckState = param.CheckState
	}
	if param.State != 0 {
		where.State = param.State
	}
	var purOrder []MallPurchaseOrder

	var tx *gorm.DB
	tx = global.DB.Debug().Where(where)
	if len(param.IdArr) > 0 && len(param.Perfect) > 0 {
		tx = tx.Where("id IN ?", param.IdArr).Where("perfect IN ?", param.Perfect)
	} else if len(param.IdArr) > 0 {
		tx = tx.Where("id IN ?", param.IdArr)
	} else if len(param.Perfect) > 0 {
		tx = tx.Where("perfect IN ?", param.Perfect)
	}
	tx.Order(page.Sortfield + " " + page.Sort).Offset(page.Page).Limit(page.Limit).Find(&purOrder)
	if tx.Error != nil {
		return nil, tx.Error
	}
	var resPurOrderArr []ResponsePurchaseOrder
	purchaseOrderLen := len(purOrder)
	if purchaseOrderLen > 0 {

		var pLangM *LangModel = new(LangModel)
		var fM *FileModel = new(FileModel)

		var purchaseOrderWg sync.WaitGroup
		purchaseOrderWg.Add(purchaseOrderLen)
		for i := 0; i < purchaseOrderLen; i++ {
			m.manyTabData(m.TabName(), purOrder[i], pLangM, fM, &resPurOrderArr, &purchaseOrderWg)
		}
		purchaseOrderWg.Wait()
	}
	return resPurOrderArr, nil
}

// 详情
func (m *PurchaseOrderModel) Detail() {

}

// 基础详情
func (m *PurchaseOrderModel) BaseDetail() {

}

func (m *PurchaseOrderModel) manyTabData(tabName string, purOrder MallPurchaseOrder, pLangM *LangModel, fM *FileModel, resPurOrderArr *[]ResponsePurchaseOrder, wg *sync.WaitGroup) {

	var resPurOrder ResponsePurchaseOrder
	var purchaseOrderLang ResponseLang
	purchaseOrderLang, _ = pLangM.List(RequestLang{
		TableName:    tabName,
		MerchantType: 2,
		MerchantId:   purOrder.PurchaseId,
		TableId:      purOrder.Id,
		Lang:         global.LANG_COUNTRY.Lang,
		Field:        []string{"product_name", "purchase_detail"},
	})
	resPurOrder.ProductName = purchaseOrderLang.ProductName
	resPurOrder.PurchaseDetail = purchaseOrderLang.Detail
	resPurOrder.Num = purOrder.Num
	resPurOrder.ExpirationDate = purOrder.ExpirationDate

	//FILE
	var reFile ResponseFile
	reFile, _ = fM.List(RequestFile{
		MerchantType: 2,
		Lang:         global.LANG_COUNTRY.Lang,
		MerchantId:   purOrder.PurchaseId,
		ProductId:    purOrder.Id,
		UsageTypd:    []uint{4, 7, 8, 10, 13},
	})
	resPurOrder.PurchaseOrderCover = reFile.MainImg
	*resPurOrderArr = append(*resPurOrderArr, resPurOrder)
	wg.Done()
}
